package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.TianJieWalletBlockDto;
import org.itboys.mobile.entity.mysql.TianJieWalletBlock;
import org.itboys.mobile.entity.mysql.TianJieWalletBlockRecord;
import org.itboys.mobile.entity.mysql.TianJieWalletProduct;
import org.itboys.mobile.entity.mysql.TianJieWalletRecord;
import org.itboys.mobile.service.mysql.web.TianJieWalletBlockService;
import org.itboys.mobile.service.mysql.web.TianJieWalletProductService;
import org.itboys.mobile.service.mysql.web.TianJieWalletService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_区块链_钱包_记录_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/wallet/block")
public class TianJieWalletBlockController extends BaseController{
    @Autowired
    private TianJieWalletBlockService walletBlockService;
    /**
     * 主页（买入、卖出）
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/trade/index")
    public String index(HttpServletRequest request,
                        Model model){
        return "/tianjie/wallet/block/trade/index";
    }


    /**
     * 分页查询交易记录(买入/卖出)
     * @param request
     * @param response
     */
    @RequestMapping("/record/trade/select")
    public void record_trade_record(HttpServletRequest request,
                       HttpServletResponse response,
                              @RequestParam(value="status",required = false)Integer status){
        try{
            PageResult<TianJieWalletBlockRecord> result=walletBlockService.selectTradeRecord(request,status);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }
    /**
     * 分页查询交易记录(全部，按人获取)
     * @param request
     * @param response
     */
    @RequestMapping("/record/member/select")
    public void record_member_record(HttpServletRequest request,
                                    HttpServletResponse response,
                                    @RequestParam(value="memberId",required = true)Long memberId){
        try{
            PageResult<TianJieWalletBlockRecord> result=walletBlockService.selectMemberRecord(request,memberId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 修改自动卖出天数
     * @param request
     * @param response
     * @param memberIds
     * @param saleDay
     */
    @RequestMapping("/update")
    public void update(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="memberIds",required = true)List<Long> memberIds,
            @RequestParam(value="saleDay",required = true)Integer saleDay){
        try {
            walletBlockService.update_wallet_block(memberIds,saleDay);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/block/trade/confirm------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 确认交易（买入卖出）
     * @param request
     * @param response
     * @param walletBlockRecordIds
     */
    @RequestMapping("/trade/confirm")
    public void trade_confirm(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="walletBlockRecordIds",required = true)List<Long> walletBlockRecordIds){
        try {
            walletBlockService.trade_confirm(walletBlockRecordIds);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/block/trade/confirm------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }






}
